package unsch.secretaria.servlet;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperRunManager;
import unsch.secretaria.ds.DSConexion;

@WebServlet(name = "SReporteCuentaUsuario", urlPatterns = {"/SReporteCuentaUsuario"})
public class SReporteProveido extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection;
        ServletOutputStream servletOutputStream =
                response.getOutputStream();
        InputStream reportStream =
                getServletConfig().getServletContext().getResourceAsStream("/reporte/ReporteGrados.jasper");
        try {
            ServletContext ctx = this.getServletConfig().getServletContext();
            DSConexion conn = new DSConexion();
            connection = conn.getConectar();
            JasperRunManager.runReportToPdfStream(reportStream,
                    servletOutputStream, new HashMap(), connection);
            connection.close();
            response.setContentType("application/pdf");
            servletOutputStream.flush();
            servletOutputStream.close();
        } catch (Exception e) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            response.setContentType("text/plain");
            response.getOutputStream().print(stringWriter.toString());
        }
    }
}
